package springboot.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AsyncService {

    @Autowired
    private ThreadPoolTaskExecutor doSomethingExecutor;

    // 指定使用beanname为doSomethingExecutor的线程池
    @Async("doSomethingExecutor")
    public String doSomething(String message) {
        log.info("do something, message={}", message);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("do something error: ", e);
        }
        return message;
    }

    public void doSomething2(String message) {
        doSomethingExecutor.submit(() -> {
            log.info("do something2, message={}", message);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                log.error("do something2 error: ", e);
            }
        });
    }
}